package com.itggm.service.impl;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itggm.DTO.UserPageDTO;
import com.itggm.mapper.UserMapper;
import com.itggm.pojo.User;
import com.itggm.response.ResponseResult;
import com.itggm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月15日 10:51
 * 用户管理
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public ResponseResult login(String username, String password, String code, HttpServletRequest request) {

        //  比较验证码
        String sessionCode = (String) request.getSession().getAttribute("code");
        if (!sessionCode.equals(code)) {
            return ResponseResult.fail().message("验证码错误");
        }

        // 根据用户名查询用户信息
        User user = userMapper.selectUserByUserName(username);
        if (ObjectUtils.isEmpty(user)) {
            return ResponseResult.fail().message("用户名错误");
        }

        // 比较密码
        String dbPassword = user.getPassword();
        String formPassword = SecureUtil.md5(password);
        if (!StringUtils.equals(dbPassword, formPassword)) {
            return ResponseResult.fail().message("密码错误");
        }

        //  保存用户信息在session
        request.getSession().setAttribute("user", user);
        // authentication  用于后续非法请求的拦截
        request.getSession().setAttribute("authentication", "true");

        // 校验通过   获取用户的昵称  返回前端
        String nickname = user.getNickname();

        return ResponseResult.success().message("认证通过").data(nickname);
    }


    @Override
    public ResponseResult personalInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();

        User usr = userMapper.selectUserByUserName(username);
        return ResponseResult.success().data(usr);
    }

    @Override
    public ResponseResult editPersonalInfo(User user) {


        userMapper.editPersonalInfo(user);

        return ResponseResult.success().message("个人信息编辑成功");
    }

    @Override
    public ResponseResult editPersonalPwd(String oldPassword, String newPassword, HttpSession session) {

        // 判定用户的旧密码是否正确
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        User usr = userMapper.selectUserByUserName(username);
        String dbPassword = usr.getPassword();
        String formPassword = SecureUtil.md5(oldPassword);

        if (!StringUtils.equals(dbPassword, formPassword)) {
            return ResponseResult.fail().message("原始密码不正确");
        }

        String newDBPassword = SecureUtil.md5(newPassword);

        userMapper.editPersonalPwd(usr.getId(), newDBPassword);

        return ResponseResult.success().message("密码编辑成功");
    }

    @Override
    public ResponseResult page(UserPageDTO userPageDTO) {
        //开启分页
        PageHelper.startPage(userPageDTO.getPage(), userPageDTO.getLimit());
        // 查询
        List<User> users = userMapper.userPage(userPageDTO.getNickname());
        // 封装分页信息
        PageInfo<User> pageInfo = new PageInfo<>(users);
        long total = pageInfo.getTotal();
        List<User> userList = pageInfo.getList();
        // 返回分页信息
        return ResponseResult.success().count(total).data(userList);

    }

    @Override
    public ResponseResult addUser(User user) {
        // 校验用户名是否重复
        User username = userMapper.selectUserByUserName(user.getUsername());
        if (!ObjectUtils.isEmpty(username)) {
            return ResponseResult.fail().message("用户名重复,添加失败");
        }
        //设置创建时间
        Date createTime = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        user.setCreateTime(createTime);

        //设置默认密码为123456
        String password = SecureUtil.md5("123456");
        user.setPassword(password);
        userMapper.addUser(user);

        return ResponseResult.success().message("添加用户成功");
    }

    @Override
    public ResponseResult deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.delete(id);
        }
        return ResponseResult.success().message("删除用户成功");
    }

    @Override
    public ResponseResult editUser(User user) {
        //设置更新时间
        Date updateTime = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        user.setUpdateTime(updateTime);
        // 更新
        userMapper.editUser(user);
        // 返回
        return ResponseResult.success().message("更新用户信息成功");
    }

    @Override
    public ResponseResult resetPassword(Integer id) {


        userMapper.resetPwd(id, SecureUtil.md5("123456"));

        return ResponseResult.success();
    }


}
